﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;
using System.Data.Entity.Infrastructure;

namespace Sovigaz.Permission.Repositories
{
    public class InOutRepository : GenericRepository
    {
        public InOutRepository()
            : base("InOutID")
        {
        }

        public ICollection<InOutComplex> InOuts { get; set; }

        #region Get

        public InOutRepository GetList(string whereClause, string orderBy, string orderDirection, int pageNumber)
        {
            var query = _context.InOuts
                .Join(_context.DeliveryPlans, i => i.DeliveryPlanID, d => d.DeliveryPlanID, (i,d) =>new {i,d})
                .Join(_context.Users,id=>id.i.UserID,u=>u.UserID,
                (id, u) => new InOutComplex()
                {
                    InOutID = id.i.InOutID,
                    UserID=id.i.UserID,                    
                    DeliveryPlanID = id.i.DeliveryPlanID,
                    InOutCode = id.i.InOutCode,
                    UserFullName=u.FullName,
                    Date = id.i.Date,
                    Type = id.i.Type,
                    Status = id.i.Status,
                    Description = id.i.Description,
                    DeliveryPlanCode = id.d.DeliveryPlanCode,
                    IsDeleted = id.i.IsDeleted
                })
                .Where(whereClause)
                .OrderBy(String.Format("{0} {1}", orderBy.Trim(), orderDirection.Trim()))
                .ToPagedList(pageNumber, DefaultPageSize);

            TotalItemCount = query.TotalItemCount;
            TotalItemOnPage = query.Count;
            PageCount = query.PageCount;
            PageNumber = query.PageNumber;
            InOuts = query.ToList();
            return this;
        }

        //public InOut GetByID(int id)
        //{            
        //    return _context.InOuts.FirstOrDefault(p => p.InOutID == id && !p.IsDeleted);
        //}

        public InOutComplex GetByID(int index)
        {
            var query = _context.InOuts
                .Join(_context.DeliveryPlans, i => i.DeliveryPlanID, d => d.DeliveryPlanID, (i, d) => new { i, d })
                .Join(_context.Users, id => id.i.UserID, u => u.UserID,
                (id, u) => new InOutComplex()
                {
                    InOutID = id.i.InOutID,
                    UserID = id.i.UserID,
                    DeliveryPlanID = id.i.DeliveryPlanID,
                    InOutCode = id.i.InOutCode,
                    UserFullName = u.FullName,
                    Date = id.i.Date,
                    Type = id.i.Type,
                    Status = id.i.Status,
                    Description = id.i.Description,
                    DeliveryPlanCode = id.d.DeliveryPlanCode,
                    IsDeleted = id.i.IsDeleted
                });
            return query.FirstOrDefault(q => q.InOutID == index && !q.IsDeleted);
        }

        #endregion

        public void Delete(int id)
        {
            var item = GetByID(id);
            if (item != null)
                item.IsDeleted = true;

            _context.SaveChanges();
        }

        public void Delete(string[] idArr)
        {
            foreach (var id in idArr)
            {
                var item = GetByID(ConvertUtility.IntParse(id));
                if (item != null)
                    item.IsDeleted = true;
            }

            _context.SaveChanges();
        }

        //public int Update(InOut item)
        //{
        //    var update = new InOut();

        //    using (var scope = new TransactionScope())
        //    {
        //        if (item.InOutID > 0)
        //        {
        //            update = GetByID(item.InOutID);
        //            if (!Utility.ArrayEqual(update.Tstamp, item.Tstamp))
        //                throw ExceptionHelper.CreateHttpResponseException("Dữ liệu đã bị cập nhật bởi người sử dụng khác.");
        //        }
        //        update.InOutID = item.InOutID;
        //        update.InOutCode = item.InOutCode;
        //        update.DeliveryPlanID = item.DeliveryPlanID;
        //        update.Date = item.Date;
        //        update.Type = item.Type;
        //        update.Status = item.Status;
        //        update.Description = item.Description;

        //        if (update.InOutID == 0)
        //            _context.InOuts.Add(update);

        //        _context.SaveChanges();
        //        _context.Entry(update).Reload();
        //        //var orderLinerepository = new InOutLineRepository();
        //        //orderLinerepository.Update(item.InOutLines, update.InOutID, scope);
        //        scope.Complete();
        //    }

        //    return update.InOutID;
        //}

        public int GetNextIndex()
        {
            if (_context.InOuts.FirstOrDefault() == null) return 1;
            return _context.InOuts.Max(p => p.InOutID) + 1;
        }
    }
}
